'''
Created on 18/11/2011

@author: Byron
'''
from tkinter import Canvas
from Constants import *

class Tablero:

    def __init__(self, ventana):    
        #Crear canvas
        self._CanvasTablero  = Canvas(ventana, 
                                      width=ANCHO_CASILLA*CNT_CASILLAS, 
                                      height=LARGO_CASILLA*CNT_CASILLAS, 
                                      bg=COLOR_WHITE)
        #Ubicar canvas
        self._CanvasTablero.place(x=30, y=20)
        self._CanvasTablero.bind("<Button-1>",self.tableroEventHandler)
        #self.colorearTablero()
        
    def colorearTablero(self,tablero):
        cntRow = 0;
        while(cntRow < CNT_CASILLAS):  
            cntCol = 0;
            while(cntCol < CNT_CASILLAS):
                if( (cntRow+cntCol)%2 == 0 ):
                    self._CanvasTablero.create_rectangle(cntRow*ANCHO_CASILLA+0, 
                                                         cntCol*LARGO_CASILLA+0, 
                                                         cntRow*ANCHO_CASILLA+ANCHO_CASILLA, 
                                                         cntCol*LARGO_CASILLA+LARGO_CASILLA, 
                                                         fill=COLOR_BLACK)
                    ficha = tablero[cntCol][cntRow] 
                    if ficha != None:
                        if ficha.getTeam() == TEAM_WHITE:
                            self._CanvasTablero.create_oval(cntRow*ANCHO_CASILLA+5, 
                                                         cntCol*LARGO_CASILLA+5, 
                                                         cntRow*ANCHO_CASILLA+ANCHO_CASILLA-5, 
                                                         cntCol*LARGO_CASILLA+LARGO_CASILLA-5, 
                                                         fill=COLOR_WHITE)
                        elif ficha.getTeam() == TEAM_BLACK:
                            self._CanvasTablero.create_oval(cntRow*ANCHO_CASILLA+5, 
                                                         cntCol*LARGO_CASILLA+5, 
                                                         cntRow*ANCHO_CASILLA+ANCHO_CASILLA-5, 
                                                         cntCol*LARGO_CASILLA+LARGO_CASILLA-5, 
                                                         fill=COLOR_RED)
                        if ficha.getType() == CORONA:
                            self._CanvasTablero.create_oval(cntRow*ANCHO_CASILLA+10, 
                                                         cntCol*LARGO_CASILLA+10, 
                                                         cntRow*ANCHO_CASILLA+ANCHO_CASILLA-10, 
                                                         cntCol*LARGO_CASILLA+LARGO_CASILLA-10, 
                                                         fill=COLOR_BLACK)
                        
                cntCol = cntCol + 1;
            cntRow = cntRow + 1;
    
    def tableroEventHandler(self,event):
        if self._GameManager.actualizarTablero(event.x//ANCHO_CASILLA,event.y//LARGO_CASILLA):
            self.colorearTablero(self._GameManager.getTablero())
        
    
    def setGameManager(self,pGameManager):
        self._GameManager = pGameManager
        
            